package org.bits.io;

import java.io.File;
import java.io.IOException;

import org.bits.data.UserOptions;
import org.jutils.io.XStreamUtils;

/***************************************************************************
 * 
 ******************************************************************************/
public class UserOptionsSerializer
{
    /**  */
    private File optionsFile;
    /**  */
    private UserOptions options;

    /***************************************************************************
     * @param file
     **************************************************************************/
    public UserOptionsSerializer( File file )
    {
        optionsFile = file;
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    public UserOptions getOptions()
    {
        if( options == null )
        {
            read();
        }

        return options;
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    public UserOptions read()
    {
        // try
        // {
        // options = ( UserOptions )IOUtils.readObjectXStream( optionsFile );
        // }
        // catch( IOException e )
        // {
        // options = UserOptions.createDefaultOptions();
        // write( options );
        // }

        // TODO Remove this next line once the user is able to create their own
        // filters.
        options = UserOptions.createDefaultOptions();

        return options;
    }

    /***************************************************************************
     * @param options
     **************************************************************************/
    public void write( UserOptions options )
    {
        try
        {
            XStreamUtils.writeObjectXStream( options, optionsFile );
        }
        catch( IOException ex )
        {
            System.out.println( "WARNING: Cannot write options file" );
            ex.printStackTrace();
        }
    }
}
